<?php

namespace App\Http\Controllers;

use App\Template;
use Illuminate\Http\Request;

class PageController extends Controller
{

    public function index()
    {
        return view('page.index')->with('pages', Template::all());
    }

    public function show(Request $request, $id)
    {
        return view('page.show')->with('data', Template::findOrFail($id));
    }

    public function collect(Request $request)
    {
        $url = "url={$request->input('url')}";
        $opts = array(
            'http'=>array(
                'method'=>"POST",
                'header'=>"Content-type: application/x-www-form-urlencoded\r\n".
                    "Content-length:".strlen($url)."\r\n" .
                    "Cookie: foo=bar\r\n" .
                    "\r\n",
                'content' => $url,
            )
        );
        file_get_contents('http://localhost:8080/collect', false, stream_context_create($opts));
        return redirect('/');
    }

    public function collectForm()
    {
        return view('page.collect');
    }
}
